﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views.Grid;
using MicropleSoft.Controls;
using MicropleSoft.Controls.DialogBox;
using MicropleSoft.Controls.Util;
using MicropleSoft.WmsApp.Base.Business;
using MicropleSoft.WmsApp.Base.Window;
using MicropleSoft.WmsApp.Library;
using MicropleSoft.WmsApp.Library.ClientProxy;
using MicropleSoft.WmsApp.Library.Proxy;
using MicropleSoft.WmsApp.Library.ServiceFacade;

namespace MicropleSoft.WmsApp.PurchaseOrder
{
    public partial class FormPurchaseOrder : BaseBusinessForm
    {
        private readonly IBusinessModelControl<PurchaseOrderWrap> _businessModelControl = null;
           
        private readonly ServerProxy _serverProxy = ServerProxyFactory.GetServerProxy();
        public FormPurchaseOrder()
        {
            InitializeComponent();
            XGridControlStyle.SetXGridControlsStyle(this.xGridControl1,this.xGridControl2,this.xGridControl3);

            _businessModelControl = new PurchaseOrderBusinessModelControl(purchaseOrderEditor1, xGridControl3);

            this.xSimpleButtonCompose1.XSimpleButtonClick += (o, e) => XSimpleButtonComposeClick1((XSimpleButton)o);

            this.xSimpleButtonCompose2.XSimpleButtonClick += (o, e) => XSimpleButtonComposeClick2((XSimpleButton)o);

            this.xSimpleButtonCompose3.XSimpleButtonClick += (o, e) => XSimpleButtonComposeClick3((XSimpleButton)o);

            LoadButtons();

            BindRespositoryItem();
        }

        private void LoadButtons()
        {
            xSimpleButtonCompose1.AddXSimpleButtonWrap(new XSimpleButtonCompose.XSimpleButtonWrap() { ButtonId = 1, ButtonName = "p11", ButtonText = "查询采购订单", ImageIndex = -1, Index = 0 });
            xSimpleButtonCompose1.AddXSimpleButtonWrap(new XSimpleButtonCompose.XSimpleButtonWrap() { ButtonId = 2, ButtonName = "p12", ButtonText = "新增采购订单", ImageIndex = -1, Index = 1 });
            xSimpleButtonCompose1.AddXSimpleButtonWrap(new XSimpleButtonCompose.XSimpleButtonWrap() { ButtonId = 3, ButtonName = "p13", ButtonText = "编辑...", ImageIndex = -1, Index = 2 });
            xSimpleButtonCompose1.LoadButtons();

            xSimpleButtonCompose2.AddXSimpleButtonWrap(new XSimpleButtonCompose.XSimpleButtonWrap() { ButtonId = 1, ButtonName = "p21", ButtonText = "查询采购明细", ImageIndex = -1, Index = 0 });
            xSimpleButtonCompose2.AddXSimpleButtonWrap(new XSimpleButtonCompose.XSimpleButtonWrap() { ButtonId = 2, ButtonName = "p22", ButtonText = "新增采购订单", ImageIndex = -1, Index = 1 });
            xSimpleButtonCompose2.AddXSimpleButtonWrap(new XSimpleButtonCompose.XSimpleButtonWrap() { ButtonId = 3, ButtonName = "p23", ButtonText = "编辑...", ImageIndex = -1, Index = 2 });
            xSimpleButtonCompose2.LoadButtons();

            xSimpleButtonCompose3.AddXSimpleButtonWrap(new XSimpleButtonCompose.XSimpleButtonWrap() { ButtonId = 1, ButtonName = "p31", ButtonText = "新增采购订单", ImageIndex = -1, Index = 0 });
            xSimpleButtonCompose3.AddXSimpleButtonWrap(new XSimpleButtonCompose.XSimpleButtonWrap() { ButtonId = 2, ButtonName = "p32", ButtonText = "保存", ImageIndex = -1, Index = 1 });
            xSimpleButtonCompose3.AddXSimpleButtonWrap(new XSimpleButtonCompose.XSimpleButtonWrap() { ButtonId = 2, ButtonName = "p32.1", ButtonText = "保存并新增", ImageIndex = -1, Index = 2 });
            xSimpleButtonCompose3.AddXSimpleButtonWrap(new XSimpleButtonCompose.XSimpleButtonWrap() { ButtonId = 3, ButtonName = "p33", ButtonText = "新增采购明细", ImageIndex = -1, Index = 3 });
            xSimpleButtonCompose3.AddXSimpleButtonWrap(new XSimpleButtonCompose.XSimpleButtonWrap() { ButtonId = 3, ButtonName = "p34", ButtonText = "编辑采购明细", ImageIndex = -1, Index = 4 });
            xSimpleButtonCompose3.LoadButtons();
        }

        private void BindRespositoryItem()
        {
            //绑定LookUp
            var gridView = (GridView)this.xGridControl1.MainView;
            LookUpBind.BindingLookUpEdit4Grid(gridView, gridView.Columns.ColumnByFieldName("Status"), "PurchaseOrder-Status.xml");
            gridView.Columns.ColumnByFieldName("Status").ColumnEdit.ReadOnly = true;

            LookUpBind.BindingLookUpEdit4Grid(gridView, gridView.Columns.ColumnByFieldName("PoType"), "PurchaseOrder-PoType.xml");
            gridView.Columns.ColumnByFieldName("PoType").ColumnEdit.ReadOnly = true;
        }

        private void XSimpleButtonComposeClick1(XSimpleButton xSimpleButton)
        {
            var xSimpleButtonWrap = (XSimpleButtonCompose.XSimpleButtonWrap) xSimpleButton.ExtendTag;
            switch(xSimpleButtonWrap.ButtonName)
            {
                case "p11":
                    try
                    {
                        var list = _serverProxy.Query<Library.ServiceFacade.PurchaseOrder>("GetPurchaseOrderList", "and 1=1");
                        this.xGridControl1.DataSource = list;
                       
                    }
                    catch (Exception exception)
                    {
                        CustomMessageBox.ShowError(exception.Message);
                    }
                    break;
                case "p12":
                    _businessModelControl.NewWrap();
                    break;
                case "p13":
                    try
                    {
                        var row = this.gridView1.GetFocusedRow();
                        if (row != null && row is Library.ServiceFacade.PurchaseOrder)
                        {
                            var purchaseOrder = (Library.ServiceFacade.PurchaseOrder)row;
                            //注意，这句话一定要在LoadWrap之前调用，因为界面加载后，第三个Tab还没有渲染，当加载Wrap的时候
                            //执行绑定，由于第三个Tab的LookUp系列空间还没有渲染，导致绑定后EditValue还不存在，会引发
                            //LookUpEditValueChanged重复执行的问题。
                            this.xXtraTabControl.SelectedTabPage = this.xtraTabPage3;
                            _businessModelControl.LoadWrap(purchaseOrder.PoId);
                        }
                    }
                    catch (Exception exception)
                    {
                        CustomMessageBox.ShowError(exception.Message);
                    }
                    break;
                default:
                    break;
            }
        }

        private void XSimpleButtonComposeClick2(XSimpleButton xSimpleButton)
        {
            var xSimpleButtonWrap = (XSimpleButtonCompose.XSimpleButtonWrap)xSimpleButton.ExtendTag;
            switch (xSimpleButtonWrap.ButtonName)
            {
                case "p21":
                    //查询明细
                    break;
                case "p22":
                    _businessModelControl.NewWrap();
                    break;
                case "p23":
                    try
                    {
                        var row = this.gridView2.GetFocusedRow();
                        if (row != null && row is Library.ServiceFacade.PurchaseOrderDetail)
                        {
                            var purchaseOrder = (Library.ServiceFacade.PurchaseOrderDetail)row;
                            //注意，这句话一定要在LoadWrap之前调用，因为界面加载后，第三个Tab还没有渲染，当加载Wrap的时候
                            //执行绑定，由于第三个Tab的LookUp系列空间还没有渲染，导致绑定后EditValue还不存在，会引发
                            //LookUpEditValueChanged重复执行的问题。
                            this.xXtraTabControl.SelectedTabPage = this.xtraTabPage3;
                            _businessModelControl.LoadWrap(purchaseOrder.PoId);
                        }
                    }
                    catch (Exception exception)
                    {
                        CustomMessageBox.ShowError(exception.Message);
                    }
                    break;
                default:
                    break;
            }
        }

        private void XSimpleButtonComposeClick3(XSimpleButton xSimpleButton)
        {
            var xSimpleButtonWrap = (XSimpleButtonCompose.XSimpleButtonWrap)xSimpleButton.ExtendTag;
            switch (xSimpleButtonWrap.ButtonName)
            {
                case "p31":
                    try
                    {
                        _businessModelControl.NewWrap();
                    }
                    catch (Exception exception)
                    {
                        CustomMessageBox.ShowError(exception.Message);
                    }
                    break;
                case "p32":
                    try
                    {
                        _businessModelControl.SaveWrap(_businessModelControl.GetCurrentWrap());
                    }
                    catch (Exception exception)
                    {
                        CustomMessageBox.ShowError(exception.Message);
                    }
                    break;
                case "p32.1":
                    try
                    {
                        _businessModelControl.SaveWrap(_businessModelControl.GetCurrentWrap());
                        _businessModelControl.NewWrap();
                    }
                    catch (Exception exception)
                    {
                        CustomMessageBox.ShowError(exception.Message);
                    }
                    break;
                case "p33":
                    try
                    {
                        _businessModelControl.AddDetail();
                    }
                    catch (Exception exception)
                    {
                        CustomMessageBox.ShowError(exception.Message);
                    }
                    break;
                case "p34":
                    try
                    {
                        _businessModelControl.EditDetail();
                    }
                    catch (Exception exception)
                    {
                        CustomMessageBox.ShowError(exception.Message);
                    }
                    break;
                default:
                    break;
            }
        }

    }
}
